package cn.itsource.template;

import cn.hutool.http.HttpUtil;
import cn.itsource.domain.OpenIdResult;
import cn.itsource.properties.WechatProperties;
import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;

/**
 * @program fccar-two
 * @Description
 * @Author TT
 * @Date 2024/09/05 16:38
 * @Version 1.0
 */
@Component
public class WechatTemplate {
    @Autowired
    private WechatProperties wechatProperties;

    public WechatTemplate(WechatProperties wechatProperties) {
        this.wechatProperties = wechatProperties;
    }

    public String getOpenId(String code){
        String url = String.format(WechatProperties.WUrl, wechatProperties.getAppid(), wechatProperties.getSecret(),code);
        String s = HttpUtil.get(url);
        OpenIdResult openIdResult = JSON.parseObject(s, OpenIdResult.class);
        return openIdResult.getOpenid();
    }
}
